<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($_SERVER['DOCUMENT_ROOT']."/Modules/MGK20XF5/moduleconf.php");
MYDCONN();
	# DEFAULT VARS
	$ext = '';
	$prefix = date("Y").'_';
	$dir = $GLOBALS['DOC'].'CCENTDOC/';
	$_getLast[1]['DocRefer'] = 1;
if (isset($_FILES['File'])) {
	if($_FILES['File']['name'] != '') {
	require_once($GLOBALS['OBJ'].'obj.files.mex');
	$_FILES['File']['name'] = strtolower(str_replace(' ','_',$_FILES['File']['name']));
	$NEWFILE = $prefix.$_FILES['File']['name'];
	$GLOBALS['MYD']->db_query("INSERT INTO doccenter_files (DOC, FILE) VALUES ({$_POST['Ref']},'{$NEWFILE}')");
	echo $ext;
	echo $prefix;
	echo $dir;
	fileimport($_FILES['File'],$ext,$prefix,$dir);
	} 

} elseif($_POST['selected'] > 0) {
	$GLOBALS['MYD']->db_query("UPDATE doccenter_files SET DOC = -1 WHERE REF = {$_POST['selected']}");
	echo '<script charset="utf-8" type="text/javascript">
	document.DocEdit.selected.value=0;</script>';
	include($MODPAD.'lib/list_showfiles.php');
} elseif (isset($_POST['Save'])) {
	$doc_text = mysql_escape_string($_POST['Body']);
	if ($_POST['Ref'] == 0) {
		$_getLast = $GLOBALS['MYD']->db_array("SELECT MAX(DocRefer) AS DocRefer FROM doccenter_document WHERE TYPE = {$_POST['TYPE']}");
		if($GLOBALS['MYD']->db_query("INSERT INTO doccenter_document (Title, Body, Time, Author, DocRefer, Attrib, TYPE,ddocArea)
			VALUES ('{$_POST['Title']}','{$doc_text}','{$sysTime}','{$_POST['Author']}',".($_getLast[1]['DocRefer']+1).",{$_POST['Attrib']},{$_POST['TYPE']},'{$_POST['ddocArea']}')")==true) {
		$GETID = $GLOBALS['MYD']->db_array("SELECT REF FROM doccenter_document WHERE TYPE = {$_POST['TYPE']} && Time = '{$sysTime}' LIMIT 1");
		$GLOBALS['MYD']->db_query("UPDATE doccenter_files SET DOC = {$GETID[1]['REF']} WHERE DOC = {$_POST['Ref']}");
		}
	} else {
		$GLOBALS['MYD']->db_query("UPDATE doccenter_document SET
			Title = '{$_POST['Title']}', 
			Body = '{$doc_text}', 
			Author = '{$_POST['Author']}', 
			Attrib = {$_POST['Attrib']}, 
			TYPE = {$_POST['TYPE']},
			STATUS = {$_POST['STATUS']}
			WHERE Ref = {$_POST['Ref']}");
	}
		echo '<div class="Confirm">Guardado com sucesso.</div>';
}
